import Vue from 'vue'
import Router from 'vue-router'
import approval from './modules/approval'
import attendance from './modules/attendance'
import department from './modules/department'
import employee from './modules/employee'
import permission from './modules/permission'
import role from './modules/role'
import salary from './modules/salary'
import social from './modules/social'

Vue.use(Router)

/* Layout */
import Layout from '@/layout' // 直接加载

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'), // 懒加载
    // meta: { title: '嘻嘻', icon: 'bug' }
    hidden: true // 确认需要隐藏(左侧菜单)
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },

  department,
  role,
  employee,
  permission,

  approval,
  attendance,
  salary,
  social,

  // 如果按照常规的写法 所有的业务页面如首页 组织 角色  员工等都得写一起 而且每个业务不仅仅只有一个页面
  // 好维护 希望所有的子页面单独成一级
  // 拆出来的代价: 先展示一级layout  再通过子路由去展示二级 首页 组织 角色  员工等

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true },

  {
    path: '/playground',
    component: Layout,
    children: [
      {
        path: '/playground',
        component: () => import('@/views/playground'),
        meta: { title: '操场', icon: 'bug' }
      }
    ]
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
